<?php

namespace app\common\model;

use PDOStatement;
use think\Collection;
use think\Model;

class Region extends Model
{

    protected mixed $config = [
            'cache' => 20140210,
            'field' => 'id,name',
            'order' => 'adcode asc',
    ];

    public function __construct($data = [])
    {
        parent::__construct($data);

        $config = config('region');
        if ($config) {
            if (!isset($config['cache']) || !isset($config['field']) || !isset($config['order'])) {
                throw new \Exception('配置不完整');
            }
            $this->config = $config;
        }
    }

    /**
     * 获取地区
     * @param int $parent_id
     * @return array|PDOStatement|string|Collection
     */
    public function getRegion(int $parent_id = 0): Collection|array|string|PDOStatement
    {
        return $this
                ->where('parent_id', $parent_id)
                ->field($this->config['field'])
                ->cache($this->config['cache'])
                ->order($this->config['order'])
                ->select();
    }


    /**
     * 获取省份
     * @return \think\model\Collection|Collection
     */
    public function getProvince(): \think\model\Collection|Collection
    {
        return $this
                ->where('level', 1)
                ->field($this->config['field'])
                ->cache($this->config['cache'])
                ->order($this->config['order'])
                ->select();
    }

    /**
     * 获取城市
     * @param $parent_id
     * @return Collection|\think\model\Collection
     */
    public function getCity($parent_id): \think\model\Collection|Collection
    {
        return $this
                ->where('parent_id', $parent_id)
                ->where('level', 2)
                ->field($this->config['field'])
                ->cache($this->config['cache'])
                ->order($this->config['order'])
                ->select();
    }

    /**
     * 获取区县
     * @param $parent_id
     * @return \think\model\Collection|Collection
     */
    public function getDistrict($parent_id): \think\model\Collection|Collection
    {
        return $this
                ->where('parent_id', $parent_id)
                ->where('level', 3)
                ->field($this->config['field'])
                ->cache($this->config['cache'])
                ->order($this->config['order'])
                ->select();
    }

    /**
     * 获取街道
     * @param $parent_id
     * @return \think\model\Collection|Collection
     */
    public function getStreet($parent_id): \think\model\Collection|Collection
    {
        return $this
                ->where('parent_id', $parent_id)
                ->where('level', 4)
                ->field($this->config['field'])
                ->cache($this->config['cache'])
                ->order($this->config['order'])
                ->select();
    }

    /**
     * 搜索地区
     * @param $keywords
     * @param int $parent_id
     * @return \think\model\Collection|Collection
     */
    public function searchRegion($keywords, int $parent_id = 0): \think\model\Collection|Collection
    {
        return $this
                ->where('parent_id', $parent_id)
                ->whereLike('name|initial|pinyin', '%' . $keywords . '%')
                ->field($this->config['field'])
                ->cache($this->config['cache'])
                ->order($this->config['order'])
                ->select();
    }

    /**
     * 搜索省份
     * @param $keywords
     * @return \think\model\Collection|Collection
     */
    public function searchProvince($keywords): \think\model\Collection|Collection
    {
        return $this
                ->where('level', 1)
                ->whereLike('name|initial|pinyin', '%' . $keywords . '%')
                ->field($this->config['field'])
                ->cache($this->config['cache'])
                ->order($this->config['order'])
                ->select();
    }

    /**
     * 搜索城市
     * @param $keywords
     * @param int $parent_id
     * @return \think\model\Collection|Collection
     */
    public function searchCity($keywords, int $parent_id = 0): \think\model\Collection|Collection
    {
        return $this
                ->where('level', 2)
                ->where('parent_id', $parent_id)
                ->whereLike('name|initial|pinyin', '%' . $keywords . '%')
                ->field($this->config['field'])
                ->cache($this->config['cache'])
                ->order($this->config['order'])
                ->select();
    }

    /**
     * 搜索区县
     * @param $keywords
     * @param int $parent_id
     * @return Collection|\think\model\Collection
     */
    public function searchDistrict($keywords, int $parent_id = 0): \think\model\Collection|Collection
    {
        return $this
                ->where('level', 3)
                ->where('parent_id', $parent_id)
                ->whereLike('name|initial|pinyin', '%' . $keywords . '%')
                ->field($this->config['field'])
                ->cache($this->config['cache'])
                ->order($this->config['order'])
                ->select();
    }

    /**
     * 搜索街道
     * @param $keywords
     * @param int $parent_id
     * @return \think\model\Collection|Collection
     */
    public function searchStreet($keywords, int $parent_id = 0): \think\model\Collection|Collection
    {
        return $this
                ->where('level', 4)
                ->where('parent_id', $parent_id)
                ->whereLike('name|initial|pinyin', '%' . $keywords . '%')
                ->field($this->config['field'])
                ->cache($this->config['cache'])
                ->order($this->config['order'])
                ->select();
    }

}